/*
 * Copyright (C) 1994-2021 Altair Engineering, Inc.
 * For more information, contact Altair at www.altair.com.
 *
 * This file is part of both the OpenPBS software ("OpenPBS")
 * and the PBS Professional ("PBS Pro") software.
 *
 * Open Source License Information:
 *
 * OpenPBS is free software. You can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * OpenPBS is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Commercial License Information:
 *
 * PBS Pro is commercially licensed software that shares a common core with
 * the OpenPBS software.  For a copy of the commercial license terms and
 * conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
 * Altair Legal Department.
 *
 * Altair's dual-license business model allows companies, individuals, and
 * organizations to create proprietary derivative works of OpenPBS and
 * distribute them - whether embedded or bundled with other software -
 * under a commercial license agreement.
 *
 * Use of Altair's trademarks, including but not limited to "PBS™",
 * "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
 * subject to Altair's trademark licensing policies.
 */

/**
 * @file    req_movejob.c
 *
 * @brief
 * 		req_movejob.c - function to move a job to another queue
 *
 * Included functions are:
 * 	req_movejob()
 * 	req_orderjob()
 */

#include <pbs_config.h> /* the master config generated by configure */

#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include "libpbs.h"
#include <errno.h>

#include "server_limits.h"
#include "list_link.h"
#include "attribute.h"
#include "credential.h"
#include "batch_request.h"
#include "resv_node.h"
#include "queue.h"
#include "job.h"
#include "reservation.h"
#include "log.h"
#include "pbs_error.h"
#include "hook.h"
#include "pbs_nodes.h"
#include "svrfunc.h"

/* Global Data Items: */

extern char *msg_badstate;
extern char *msg_manager;
extern char *msg_movejob;
extern int pbs_errno;

/**
 * @brief
 * 		req_movejob = move a job to a new destination (local or remote)
 *
 * @param[in,out]	req	-	the batch request
 */

void
req_movejob(struct batch_request *req)
{
	int jt; /* job type */
	job *jobp;
	char hook_msg[HOOK_MSG_SIZE];

	switch (process_hooks(req, hook_msg, sizeof(hook_msg), pbs_python_set_interrupt)) {
		case 0: /* explicit reject */
			reply_text(req, PBSE_HOOKERROR, hook_msg);
			return;
		case 1:					   /* explicit accept */
			if (recreate_request(req) == -1) { /* error */
				/* we have to reject the request, as 'req' */
				/* may have been partly modified */
				strcpy(hook_msg,
				       "movejob event: rejected request");
				log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_HOOK,
					  LOG_ERR, "", hook_msg);
				reply_text(req, PBSE_HOOKERROR, hook_msg);
				return;
			}
			break;
		case 2: /* no hook script executed - go ahead and accept event*/
			break;
		default:
			log_event(PBSEVENT_DEBUG2, PBS_EVENTCLASS_HOOK,
				  LOG_INFO, "", "movejob event: accept req by default");
	}

	jobp = chk_job_request(req->rq_ind.rq_move.rq_jid, req, &jt, NULL);

	if (jobp == NULL)
		return;

	if ((jt != IS_ARRAY_NO) && (jt != IS_ARRAY_ArrayJob)) {
		req_reject(PBSE_IVALREQ, 0, req);
		return;
	}

	if (!check_job_state(jobp, JOB_STATE_LTR_QUEUED) &&
	    !check_job_state(jobp, JOB_STATE_LTR_HELD) &&
	    !check_job_state(jobp, JOB_STATE_LTR_WAITING)) {
#ifndef NDEBUG
		log_eventf(PBSEVENT_DEBUG, PBS_EVENTCLASS_JOB, LOG_DEBUG,
			   jobp->ji_qs.ji_jobid, "(%s) %s, state=%c",
			   __func__, msg_badstate, get_job_state(jobp));
#endif /* NDEBUG */
		req_reject(PBSE_BADSTATE, 0, req);
		return;
	}

	if (jt != IS_ARRAY_NO) {
		/* cannot move Subjob and can only move array job if */
		/* no subjobs are running			     */
		if ((jt != IS_ARRAY_ArrayJob) ||
		    (jobp->ji_ajinfo->tkm_subjsct[JOB_STATE_RUNNING] != 0)) {
			req_reject(PBSE_IVALREQ, 0, req);
			return;
		}
	}

	/*
	 * svr_movejob() does the real work, handles both local and
	 * network moves
	 */

	switch (svr_movejob(jobp, req->rq_ind.rq_move.rq_destin, req)) {
		case 0: /* success */
			(void) strcpy(log_buffer, msg_movejob);
			(void) sprintf(log_buffer + strlen(log_buffer),
				       msg_manager, req->rq_ind.rq_move.rq_destin,
				       req->rq_user, req->rq_host);
			log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, LOG_INFO,
				  jobp->ji_qs.ji_jobid, log_buffer);
			reply_ack(req);
			break;
		case -1:
		case -2:
		case 1: /* fail */
			if (jobp)
				svr_evalsetjobstate(jobp);

			if (jobp && jobp->ji_clterrmsg)
				reply_text(req, pbs_errno, jobp->ji_clterrmsg);
			else
				req_reject(pbs_errno, 0, req);
			break;
		case 2: /* deferred, will be handled by 	   */
			/* post_movejob() when the child completes */
			break;
	}
	return;
}

/**
 * @brief
 * 		req_orderjob - reorder the jobs in a queue
 *
 * @param[in,out]	req	-	the batch request
 */
void
req_orderjob(struct batch_request *req)
{
	int jt1, jt2; /* job type */
	job *pjob;
	job *pjob1;
	job *pjob2;
	long long rank;
	int rc;
	char tmpqn[PBS_MAXQUEUENAME + 1];
	conn_t *conn = NULL;
	char *physhost = NULL;

	if ((pjob1 = chk_job_request(req->rq_ind.rq_move.rq_jid, req, &jt1, NULL)) == NULL)
		return;
	if ((pjob2 = chk_job_request(req->rq_ind.rq_move.rq_destin, req, &jt2, NULL)) == NULL)
		return;
	if ((jt1 == IS_ARRAY_Single) || (jt2 == IS_ARRAY_Single) ||
	    (jt1 == IS_ARRAY_Range) || (jt2 == IS_ARRAY_Range)) {
		/* can only move regular or Array Job, not Subjobs */
		req_reject(PBSE_IVALREQ, 0, req);
		return;
	}

	if (check_job_state(pjob = pjob1, JOB_STATE_LTR_RUNNING) ||
	    check_job_state(pjob = pjob2, JOB_STATE_LTR_RUNNING) ||
	    check_job_state(pjob = pjob1, JOB_STATE_LTR_BEGUN) ||
	    check_job_state(pjob = pjob2, JOB_STATE_LTR_BEGUN)) {
#ifndef NDEBUG
		log_eventf(PBSEVENT_DEBUG, PBS_EVENTCLASS_JOB, LOG_DEBUG,
			   pjob->ji_qs.ji_jobid, "(%s) %s, state=%c",
			   __func__, msg_badstate, get_job_state(pjob));
#endif /* NDEBUG */
		req_reject(PBSE_BADSTATE, 0, req);
		return;
	} else if (pjob1->ji_qhdr != pjob2->ji_qhdr) {

		/* Jobs are in different queues */

		conn = get_conn(req->rq_conn);
		if (conn) {
			physhost = conn->cn_physhost;
		}

		if ((rc = svr_chkque(pjob1, pjob2->ji_qhdr,
				     get_jattr_str(pjob1, JOB_ATR_submit_host),
				     physhost,
				     MOVE_TYPE_Order)) ||
		    (rc = svr_chkque(pjob2, pjob1->ji_qhdr,
				     get_jattr_str(pjob2, JOB_ATR_submit_host),
				     physhost,
				     MOVE_TYPE_Order))) {
			req_reject(rc, 0, req);
			return;
		}
	}

	/* now swap the order of the two jobs in the queue lists */

	rank = get_jattr_ll(pjob1, JOB_ATR_qrank);
	set_jattr_ll_slim(pjob1, JOB_ATR_qrank, get_jattr_ll(pjob2, JOB_ATR_qrank), SET);
	set_jattr_ll_slim(pjob2, JOB_ATR_qrank, rank, SET);

	if (pjob1->ji_qhdr != pjob2->ji_qhdr) {
		(void) strcpy(tmpqn, pjob1->ji_qs.ji_queue);
		(void) strcpy(pjob1->ji_qs.ji_queue, pjob2->ji_qs.ji_queue);
		(void) strcpy(pjob2->ji_qs.ji_queue, tmpqn);
		svr_dequejob(pjob1);
		svr_dequejob(pjob2);
		(void) svr_enquejob(pjob1, NULL);
		(void) svr_enquejob(pjob2, NULL);

	} else {
		swap_link(&pjob1->ji_jobque, &pjob2->ji_jobque);
		swap_link(&pjob1->ji_alljobs, &pjob2->ji_alljobs);
	}

	/* need to update disk copy of both jobs to save new order */
	job_save_db(pjob1);
	job_save_db(pjob2);

	reply_ack(req);
}
